package com.car.loan.project.xr.controller;

import com.car.loan.common.exception.loan.LoanParameterException;
import com.car.loan.framework.aspectj.lang.annotation.DataStatus;
import com.car.loan.framework.aspectj.lang.annotation.Log;
import com.car.loan.framework.aspectj.lang.enums.BusinessType;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.xr.domain.LoanAlbum;
import com.car.loan.project.xr.domain.LoanStatus;
import com.car.loan.project.xr.mapper.LoanAlbumMapper;
import com.car.loan.project.xr.service.ILoanAlbumService;
import com.car.loan.project.xr.service.ILoanService;
import com.car.loan.project.xr.service.ILoanStatusService;
import com.car.loan.project.xr.service.LoanDataAuditService;
import com.car.loan.project.xr.util.bo.business.LoanAlbumDeleteBo;
import com.car.loan.project.xr.util.bo.business.LoanBaseInfo;
import com.car.loan.project.xr.util.bo.business.LoanDataAuditBo;
import com.car.loan.project.xr.util.bo.business.payment.PaymentInfoBo;
import com.car.loan.project.xr.util.enums.CarInfoStatus;
import com.car.loan.project.xr.util.enums.DataAuditStatus;
import com.car.loan.project.xr.util.enums.FileAuditStatus;
import com.car.loan.project.xr.util.enums.LoanAlbumType;
import com.car.loan.project.xr.util.enums.PaymentSettleAccountsStatus;
import com.car.loan.project.xr.util.select.LoanBasePageParam;
import com.car.loan.project.xr.util.vo.BusinessAuditDetailVo;
import com.car.loan.project.xr.util.vo.LoanAlbumVo;
import com.car.loan.project.xr.util.vo.LoanDetailVo;
import com.car.loan.project.xr.util.vo.PaymentApplyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javax.validation.Valid;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangding
 * @description
 * @date 2019/12/8 10:33
 */
@RestController
@RequestMapping("xr/data/audit")
@Api(value = "资料管理", tags = "车贷业务-资料审核")
public class LoanDataAuditController {

    @Autowired
    private ILoanService loanService;

    @Autowired
    private ILoanStatusService loanStatusService;

    @Autowired
    private LoanDataAuditService loanDataAuditService;

    @Autowired
    private ILoanAlbumService loanAlbumService;

    @PreAuthorize("@ss.hasPermi('data:audit:list')")
    @GetMapping("list")
    @ApiOperation(value = "获取资料审核列表", response = LoanBaseInfo.class, responseContainer = "list")
    @DataStatus(loanStatusEnum = com.car.loan.common.enums.LoanStatusEnum.DATA_AUDIT_STATUS)
    public AjaxResult applyList(@Valid LoanBasePageParam loanBasePageParam) {
        return AjaxResult.success(loanService.page(loanBasePageParam));
    }


    @PreAuthorize("@ss.hasPermi('data:audit:query')")
    @GetMapping("{loanId}")
    @ApiOperation(value = "获取资料审核详情", response = BusinessAuditDetailVo.class)
    public AjaxResult saveApply(@PathVariable Long loanId) {
        return AjaxResult.success(loanService.getBusinessAuditDetailVo(loanId));
    }


    @PreAuthorize("@ss.hasPermi('data:audit:audit')")
    @PostMapping("audit")
    @Log(title = "资料审核", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "审核", notes = "如结算信息已提交的， 扣罚金额，扣罚备注只展示不可修改")
    public  AjaxResult audit(@RequestBody @Valid LoanDataAuditBo loanDataAudit) {
        return loanDataAuditService.audit(loanDataAudit);
    }


    @PreAuthorize("@ss.hasPermi('car:album:audit')")
    @PutMapping("album")
    @ApiOperation(value = "车辆附件添加")
    public synchronized  AjaxResult putImages(@Valid @RequestBody LoanAlbums loanAlbumVo) {
        List<LoanAlbumVo> loanAlbumVos = loanAlbumVo.getLoanAlbumVos();
        if (null != loanAlbumVos && loanAlbumVos.size() > 0) {
            for (LoanAlbumVo albumVo : loanAlbumVos) {
                LoanAlbum loanAlbum = new LoanAlbum();
                loanAlbum.setLoanId(albumVo.getLoanId());
                loanAlbum.setType(LoanAlbumType.CAR_INFO.type);
                loanAlbum.setUrl(albumVo.getDbUrl());
                loanAlbumService.insertLoanAlbum(loanAlbum);
            }
        }
        return AjaxResult.success();
    }

    @Data
    public static class LoanAlbums {

        List<LoanAlbumVo> loanAlbumVos = new ArrayList<>();
    }


    @PreAuthorize("@ss.hasPermi('data:audit:withdraw')")
    @PutMapping("withdraw/{loanId}")
    @Log(title = "撤回资料审核", businessType = BusinessType.WITHDRAW)
    @ApiOperation(value = "撤回资料审核", notes = "档案信息已审核后不可撤回")
    public AjaxResult withdrawApply(@PathVariable Long loanId) {
        Optional<LoanStatus> optionalLoanStatus = loanStatusService.getByLoanId(loanId);
        LoanStatus loanStatus = optionalLoanStatus
            .orElseThrow(() -> new LoanParameterException("业务状态数据异常请联系管理员"));
        Long fileAuditStatus = loanStatus.getFileAuditStatus();
        if (FileAuditStatus.SUBMIT.status == fileAuditStatus) {
            return AjaxResult.error("档案信息已审核，不可撤回");
        }
        loanStatus.setDataAuditStatus((long) DataAuditStatus.INIT.status);
        loanStatusService.updateLoanStatus(loanStatus);
        return AjaxResult.success();
    }
}
